package com.foronenet.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);

        //目前是串行的，不能并行处理多请求
        //若要处理多请求则需要启动线程，如果使用线程，最好用spring的线程池，不要自己new一个新线程
        while (true) {
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();

            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer))!=-1) {
                String str = new String(buffer, 0, len);
                System.out.println(socket.getInetAddress().getHostAddress()+":"+socket.getPort()+"=>"+str);
            }
        }
    }
}
